// pages/insurance/insurance.js
import config from "../../config.js";
const openIdUrl = config;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    bgImg: "",
    agreement: "加载协议...",
    joinlist: [],
    orderId: null,
    checkValue: 1,
    page: {
      processes: [
        {
          'name': '服务下单',
          'icon': '../../image/repair/fwxd.png'
        },
        {
          'name': '车管家到达指定地点',
          'icon': '../../image/repair/cgjddzddd.png'
        },
       /** {
          'name': '车管家到店维修',
          'icon': '../../image/repair/clsmjf.png'
        },**/
        {
          'name': '店面专业维修',
          'icon': '../../image/repair/ddbydd.png'
        },

        {
          'name': '管家严格质检',
          'icon': '../../image/repair/clsmjf.png'
        },
        {
          'name': '完成订单',
          'icon': '../../image/repair/gjygzj.png'
        },
        {
          'name': '车辆上门交付',
          'icon': '../../image/repair/dmzyby.png'
        },


       
      ]
    }
  },

  orderDetail() {
    var that = this;
    //获取订单详情
    wx.request({
      url: openIdUrl.orderDetailUrl,
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': wx.getStorageSync("session")
      },
      data: {
        id: this.data.orderId
      },
      method: "POST",
      success: function(res) {
        console.log(res.data);
        if (res.data.status == 200) {
          var rId = res.data.data.service.repaireType.typeId;
          if (rId == 2) {
            //保险修车
            setTimeout(function() {
              wx.navigateTo({
                url: '../repairBx/repairBx' + '?orderId=' + that.data.orderId,
              })
            }, 500);

          } else {
            //自己修
            setTimeout(function() {
              wx.navigateTo({
                url: '../repairZf/repairZf' + '?orderId=' + that.data.orderId,
              })
            }, 500);
          }

        } else {
          wx.showToast({
            title: '订单获取失败！',
            icon: 'none',
            duration: 1500
          })
          return false;
        }
      }

    })
  },

  getDataJoin: function() {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })

    wx.request({
      url: openIdUrl.insuranceComList,
      data: {
        id: 1,
      },
      success: function(res) {
        console.log(res);
        wx.hideLoading();
        if (res.data.status == 200) {
          console.log(res.data.data);
          that.setData({
            joinlist: res.data.data
          })

        } else {
          wx.hideLoading();
          wx.showToast({
            title: res.data.message + "",
            icon: 'none',
            mask: true,
            duration: 2000
          })
        }

      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    //加载背景图
    var services = wx.getStorageSync("services");
    if (services != null) {
      var service = services[3];
      console.log(service);
      this.setData({
        bgImg: service.pic
      })
    }
    var requestType = options.orderId;
    console.log(requestType);
    if (requestType != null) {
      this.setData({
        orderId: requestType
      })
    }

    if (requestType != "" && requestType != null) {
      this.orderDetail();
    }


    var houseKeeper = wx.getStorageSync("houseKeeper");
    if (houseKeeper) {
      this.setData({
        "defaultKeeper.name": houseKeeper.name,
        "defaultKeeper.houseKeeperId": houseKeeper.keeperId,
        "defaultKeeper.header": houseKeeper.image,
      })

      this.setData({
        "defaultKeeper2.name": houseKeeper.name,
        "defaultKeeper2.houseKeeperId": houseKeeper.keeperId,
        "defaultKeeper2.header": houseKeeper.image,
      })


      console.log(this.data.mainInfo);
    }

    var monthStr = (new Date().getMonth() + 1) < 10 ? "0" + (new Date().getMonth() + 1) : (new Date().getMonth() + 1);
    var dayStr = new Date().getDay() < 10 ? "0" + new Date().getDay() : new Date().getDay();
    var dateStr = new Date().getFullYear() + "-" + monthStr + "-" + dayStr;
    this.setData({
      date: dateStr
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getDataJoin();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  agreementDetail: function(e) {
    var id = e.currentTarget.dataset.id;
    var url = config.http + "://" + config.host + "/agreemmentContent.action?contentId=" + id;
    wx.setStorageSync("webviewUrl", url);
    wx.navigateTo({
      url: "../webview/webview"
    })
  },
  agreeCheck: function(e) {
    var isCheck = e.detail.value;
    console.log(isCheck);
    if (isCheck.length ==0) {
      isCheck = 0;
    }else{
      isCheck = 1;
    }
    console.log(isCheck);

    this.setData({
      checkValue: isCheck
    })
  },
  loadPage: function(e) {

    if (this.data.checkValue == 0) {
      wx.showToast({
        title: '请仔细阅读并同意服务协议',
        icon: 'none'
      })
      return false;
    }
    var type = e.currentTarget.dataset.type;
    wx.setStorageSync("repaireType", type);
    if (type == 1) {
      wx.navigateTo({
        url: '../repairZf/repairZf',
      })
    } else {
      wx.navigateTo({
        url: '../repairBx/repairBx',
      })
    }

  }
})